package com.technoorbit.vbreakup;




import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
 
/**
 * This Activity appears as a dialog. Users can type their name. 
 * When a user name is chosen,
 * the name is sent back to the parent
 * Activity in the result Intent.
 */
public class UserNameActivity extends Activity {
    // Debugging
    private static final String TAG = "UserNameActivity";
    private static final boolean D = true;

    // Return Intent extra
    public static String EXTRA_USER_NAME = "user_name";
    
    EditText  mEditText = null; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.user_name);

        // Set result CANCELED in case the user backs out
        setResult(Activity.RESULT_CANCELED);
        
        // Initialize the button to perform device discovery
        Button okButton = (Button) findViewById(R.id.username_ok_button);
        mEditText 		=  (EditText) findViewById(R.id.username_edittext);
        
        mEditText.setOnEditorActionListener(mWriteListener);
        
        okButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

            	decideUserName();             
            }
        });
    }
    
    
    private void decideUserName(){
    	String typedUserName = mEditText.getText().toString();
    	
    	if(typedUserName.length() <= 0){
    		return;
    	}
        // Create the result Intent and include the MAC address
        Intent intent = new Intent();
        intent.putExtra(EXTRA_USER_NAME, typedUserName);

        // Set result and finish this Activity
        setResult(Activity.RESULT_OK, intent);
        finish();	// The ActivityResult is propagated back to whoever launched you via onActivityResult().
    }
    
    
 // The action listener for the EditText widget, to listen for the return key
    private TextView.OnEditorActionListener mWriteListener =
        new TextView.OnEditorActionListener() {
        public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
            // If the action is a key-up event on the return key, send the message
            if (actionId == EditorInfo.IME_NULL && event.getAction() == KeyEvent.ACTION_UP) {
            	decideUserName();
            }
            if(D) Log.i(TAG, "END onEditorAction");
            return true;
        }
    };
    
}
